<template>
  <div class="download-dialog">
    <el-dialog
      v-model="dialogVisible"
      width="550"
      :modal="false"
      align-center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      @closed="closedHandle"
    >
      <template #header>
        <dialog-title />
      </template>
      <div class="content">
        <div class="content-title">
          <svg-icon
            icon-class="download-dialog-folder"
            :size="20"
          />
          <div class="content-title-content">
            <div
              class="title"
              :title="fileName"
            >
              文件名:  {{ fileName }}
            </div>
            <div>文件大小:  {{ fileSize }}</div>
          </div>
        </div>
        <div class="content-input-wrapper">
          <span>下载到</span>
          <el-input
            v-model="downloadPath"
            class="content-input"
            :title="downloadPath"
            readonly
          />
          <el-button
            type="default"
            style="background:transparent"
            @click="selectPath"
          >
            预览
          </el-button>
        </div>
        <div class="content-use-size">
          {{ canUsePan }} 盘剩余空间:{{ canUsePanSize }}
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-checkbox
            v-model="isDefaultPath"
            style="flex-grow: 1;"
          >
            默认此路径为下载路径
          </el-checkbox>
          <el-button
            type="default"
            class="button"
            style="width:116px;border-radius: 18px;flex-grow: 0;"
            @click="handleCancelInner('cancel')"
          >
            取消
          </el-button>
          <el-button
            type="primary"
            class="button"
            style="width:116px;border-radius: 18px;flex-grow: 0;"
            @click="handleSuccessInner()"
          >
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref,onMounted, computed, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { invokeAsyncAPi } from '@/utils/xz-fn'
import { useDownloadPath } from '@/stores/download-path'
import {getFileSizeStr} from '@/utils'

const props = defineProps({
  handleSuccess: {
    type: Function,
    default:()=>{}
  },
  handleCancel: {
    type: Function,
    default:()=>{}
  },
  fileName:{
    type: String,
    default:'',
  },
  fileSize: {
    type:String,
    default:'',
  }
})

const downloadPathStore = useDownloadPath();
const {downloadPath, isDefaultPath }=storeToRefs(downloadPathStore)

const dialogVisible = ref(true)
const clickType = ref('cancel')
const cancelType = ref('cancel')
const canUsePan = ref('');//可以使用盘的空间
const canUsePanSize = ref('');//可以使用盘的空间

const handleSuccessInner = () => {
  clickType.value = 'success'
  dialogVisible.value = false
}


const handleCancelInner = (_cancelType: string) => {
  clickType.value = 'cancel'
  cancelType.value = _cancelType
  dialogVisible.value = false
}

const closedHandle = () => {
  if (clickType.value === 'success') {
    props.handleSuccess()
  } else {
    props.handleCancel(cancelType.value)
  }
}

watch(downloadPath, async (value) => {

  canUsePan.value = value.split(':')[0] ?? '';
  const rs = await invokeAsyncAPi('fq_GetDiskFreeSpace',(canUsePan.value+':'));
  canUsePanSize.value = getFileSizeStr(rs);
  
}, {
  immediate:true
})

const selectPath = async () => {//选择路径
  const rs=await invokeAsyncAPi('fq_BrowserAndSelectFile', {
    pick_folders:true
  });
  if (rs.ret) {
    const path = rs.files[0];
    downloadPathStore.setDownloadPath(path);
  }
}
</script>

<style scoped lang="scss">
.download-dialog{
  .content{
    background: rgba(25,38,85,0.05);
    border-radius: 6px;
    padding:14px;
    .content-title{
      display: flex;
      gap:5px;
      .content-title-content{
        .title{
          text-overflow: ellipsis;
          overflow:hidden;
          white-space: nowrap;
          width:400px;
        }
      }
    }
    .content-input-wrapper{
      display: flex;
      align-items: center;
      margin:20px 0 0px;
      .content-input{
        width:250px;
        margin:0 15px;
        :deep(.el-input__wrapper){
          background:transparent;
        }
      }
    }
    .content-use-size{
      margin:10px 68px;
    }
  }
  
  .dialog-footer{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding:0 15px ;
  }
}
</style>
